package com.polaris.live.ui.live.activity

import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.common.constant.PagConst
import com.polaris.live.common.constant.PagConst.FREE_CARD_PAG
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.util.AppCodeUtils.arTo
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.ActivityNoticeMatchBinding
import com.polaris.live.im.sfs.bean.pusher.NoticeMatchMessage
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.image.loadImage
import com.polaris.live.viewmodel.live.CallVideoViewModel
import org.libpag.PAGScaleMode

/**
 * CallVideoActivity
 *
 * @author Created by lucas on 2023/11/14 09:34
 * @since 1.0.0
 */
class NoticeMatchActivity : BaseVbActivity<CallVideoViewModel, ActivityNoticeMatchBinding>() {

    private val mCallMessage by lazy {
        intent.getParcelableExtra<NoticeMatchMessage>(LiveConst.Params.NOTICE_MATCH_MESSAGE)
    }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentBar()
            .titleBarMarginTop(mBinding.rlContent)
            .init()

        NavigationBarUtils.setNavigationHeight(mBinding.rlContent)
    }

    override fun initView(savedInstanceState: Bundle?) {
        val userBasicVo = mCallMessage?.user?.base
        userBasicVo?.let {
            mBinding.nickName.text = it.nickname
            Glide.with(this)
                .loadImage(mCallMessage?.user?.cover ?: it.avatar)
                .centerCrop()
                .into(mBinding.userHead)

            mBinding.country.setCountry(it.country)
            mBinding.language.setLanguage(it.language)
            mBinding.tvMessage.text = (mCallMessage?.content ?: "").arTo()
            if (mCallMessage?.user?.actualPrice == 0L) {
                mBinding.pagFree.apply {
                    visibility = View.VISIBLE
                    path = FREE_CARD_PAG
                    setRepeatCount(-1)
                    play()
                }
                mBinding.pagFree.visibility = View.VISIBLE
            } else {
                mBinding.pagFree.visibility = View.GONE
            }
        }

        mBinding.pagAnswer.apply {
            path = PagConst.CALL_ANSWER_PAG
            setRepeatCount(-1)
            setScaleMode(PAGScaleMode.Zoom)
            play()
        }

        initObserver()
        initListener()

        mViewModel.launchAnswerTimeout()
    }

    private fun initObserver() {
        mViewModel.answerTimeoutLiveData.observe(this) {
            finish()
        }
    }

    private fun initListener() {
        mBinding.llRefuse.setOnSingleClickListener {
            finish()
        }

        mBinding.llAgree.setOnSingleClickListener {
            mCallMessage?.user?.base?.let { it1 ->
                StartCallProxy.builder(this).startNoticeCall(
                    mViewModel,
                    UserConst.From.NOTICE,
                    UserConst.LastFrom.NOTICE,
                    LiveConst.SenderFrom.INVITE,
                    it1.userId,
                    it1,
                    mCallMessage?.matchId,
                    {
                        finish()
                    }, {
                        finish()
                    }
                )
            }
        }
    }


}